﻿#include <qg/qg.h>

using namespace tpl;

#define USEOPENGL	0

// 세이더 코드
const char ShaderCode[] = \
"cbuffer Constants : register(b0)"\
"{"\
"	float4x4 sWorldViewProj;"\
"};"\
"struct VSINPUT "\
"{"\
"	float3 pos : POSITION;"\
"	float4 color : COLOR;"\
"};"\
"struct VSOUTPUT "\
"{"\
"	float4 pos : SV_Position;"\
"	float4 color : COLOR;"\
"};"\
"VSOUTPUT VS(VSINPUT i) "\
"{"\
"	VSOUTPUT o;"\
"	o.pos = mul(float4(i.pos, 1.0f), sWorldViewProj);"\
"	o.color = i.color;"\
"	return o;"\
"}"\
"float4 PS(VSOUTPUT i) : SV_Target"\
"{"\
"	return i.color;	"\
"}";

// 정점 형식
struct Vertex
{
	tpl::Vec3			pos;
	tpl::Color			color;
};

//
int main(void)
{
	k_mesg("DRAW BOX TEST\n");

	qgDevice* dev = qgDevice::New("d11", 640, 480, QGSDF_MSAA | QGSDF_LAYOUT | QGSDF_AUTO);

	if (dev)
	{
		dev->Mount(QG_WORK_PATH);

		qgStub* stub = dev->GetStub();
		qgFreeType* font = qgFreeType::New();

		// 래스터라이저
		qgRasz* rasz = dev->CreateRasz(qgRaszDataEx());

		// 레이아웃
		qgLayoutElement le[] =
		{
			{QGDLOU_POSITION, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_COLOR, 0, QGDLOT_FLOAT4, 0, 0, },
		};
		qgLayout* lo = dev->CreateLayout(le);

		// 세이더
#if !USEOPENGL
		qgShader* shd = dev->CreateShader();
		if (shd)
		{
			shd->BindData(QGSHADER_PS, ShaderCode, sizeof(ShaderCode), 0);
			shd->BindData(QGSHADER_VS, ShaderCode, sizeof(ShaderCode), 0);
			//shd->Link();
		}
#endif

		// 꼭지점
		Vertex vts[] =
		{
			{tpl::Vec3(-0.5f, -0.5f, +0.5f), tpl::Color(0.0f, 0.0f, 0.0f)},
			{tpl::Vec3(-0.5f, -0.5f, -0.5f), tpl::Color(0.0f, 0.0f, 1.0f)},
			{tpl::Vec3(-0.5f, +0.5f, +0.5f), tpl::Color(0.0f, 1.0f, 0.0f)},
			{tpl::Vec3(-0.5f, +0.5f, -0.5f), tpl::Color(0.0f, 1.0f, 1.0f)},
			{tpl::Vec3(+0.5f, -0.5f, +0.5f), tpl::Color(1.0f, 0.0f, 0.0f)},
			{tpl::Vec3(+0.5f, -0.5f, -0.5f), tpl::Color(1.0f, 0.0f, 1.0f)},
			{tpl::Vec3(+0.5f, +0.5f, +0.5f), tpl::Color(1.0f, 1.0f, 0.0f)},
			{tpl::Vec3(+0.5f, +0.5f, -0.5f), tpl::Color(1.0f, 1.0f, 1.0f)},
		};
		qgBuffer* vb = dev->CreateVertexBuffer(vts, lo, 0, 0);

		// 인덱스
		kuint idx[] =
		{
			0, 2, 1, 1, 2, 3, // -x
			4, 5, 6, 5, 7, 6, // +x
			0, 1, 5, 0, 5, 4, // -y
			2, 6, 7, 2, 7, 3, // +y
			0, 4, 6, 0, 6, 2, // -z
			1, 3, 7, 1, 7, 5  // +z
		};
		qgBuffer* ib = dev->CreateIndexBuffer(idx, 0);

		//
		float ang = 0.0f;

		//
		while (dev->Run())
		{
			float adv = stub->GetAdvance();

			ang = k_cprad(ang + KM_PI_HALF*adv);

			//tpl::Mat4 m = tpl::Mat4::Rotation(ang, ang, 0.0f);
			tpl::Mat4 m = tpl::Mat4::RotationX(ang);
			m.ReplaceLocation(0.0f, 0.0f, 4.0f);

			//
			dev->Enter();

			dev->SetTexture(0, NULL);
			dev->SetRasz(rasz);
#if !USEOPENGL
			dev->SetShader(shd);
#endif
			dev->SetLayout(lo);
			dev->SetIndexBuffer(0, ib);
			dev->SetVertexBuffer(0, vb);
			dev->SetTopology(QGTPG_TRI_LIST);
			dev->SetTM(QGTMS_WORLD, m);
			dev->DrawIndexed(36, 0);

			Rect rt(Point(50, 50), Point(256, 256));

			font->WriteFormat(5, 5, 0, "[%.2f] %.0f", stub->GetRunFps(), k_todeg(ang));

			dev->Leave();
			dev->Flush();
		}

		font->Unload();
		dev->Unload();
	}

	return 0;
}
